<?php
if (!defined('IN_PROVERB')) {
    exit('Access Denied');
}

function getTopic($topic_id)
{
    global $db;
    $query = "select topic_id,topic_name,file_id,isused,topic_intro,topic_tag,created_time from " . tname('topic') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        return $topic;
    }
    return array();
}

function getTopicDetail($topic_id)
{
    global $db;
    $query = "select * from " . tname('topic') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        return $topic;
    }
    return array();
}


function getAllTopics($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select topic_id from " . tname('topic');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by topic_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的topics
    while ($rows = $db->fetch_array($query)) {
        $topic = getTopic($rows['topic_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getNews($topic_id)
{
    global $db;
    $query = "select topic_id,topic_name,file_id,isused,topic_intro,topic_tag,created_time from " . tname('news') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        $topic['file'] = getfile($topic['file_id']);
        return $topic;
    }
    return array();
}

function getNewsDetail($topic_id)
{
    global $db;
    $query = "select * from " . tname('news') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        return $topic;
    }
    return array();
}


function getAllNews($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select topic_id from " . tname('news');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by topic_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的topics
    while ($rows = $db->fetch_array($query)) {
        $topic = getNews($rows['topic_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getXuexi($topic_id)
{
    global $db;
    $query = "select topic_id,topic_time,topic_name,file_id,isused,topic_intro,topic_tag,created_time from " . tname('xuexi') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        $topic['file'] = getfile($topic['file_id']);
        return $topic;
    }
    return array();
}

function getXuexiDetail($topic_id)
{
    global $db;
    $query = "select * from " . tname('xuexi') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        return $topic;
    }
    return array();
}


function getAllXuexi($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select topic_id from " . tname('xuexi');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by topic_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的topics
    while ($rows = $db->fetch_array($query)) {
        $topic = getXuexi($rows['topic_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getShici($topic_id)
{
    global $db;
    $query = "select topic_id,topic_name,file_id,isused,topic_intro,topic_tag,created_time from " . tname('shici') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        $topic['file'] = getfile($topic['file_id']);
        return $topic;
    }
    return array();
}

function getShiciDetail($topic_id)
{
    global $db;
    $query = "select * from " . tname('shici') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        return $topic;
    }
    return array();
}


function getAllShici($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select topic_id from " . tname('shici');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by topic_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的topics
    while ($rows = $db->fetch_array($query)) {
        $topic = getShici($rows['topic_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getErxueyizuo($topic_id)
{
    global $db;
    $query = "select topic_id,topic_name,file_id,isused,topic_intro,topic_tag,created_time from " . tname('erxueyizuo') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        $topic['file'] = getfile($topic['file_id']);
        return $topic;
    }
    return array();
}

function getErxueyizuoDetail($topic_id)
{
    global $db;
    $query = "select * from " . tname('erxueyizuo') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        return $topic;
    }
    return array();
}


function getAllErxueyizuo($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select topic_id from " . tname('erxueyizuo');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by topic_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的topics
    while ($rows = $db->fetch_array($query)) {
        $topic = getErxueyizuo($rows['topic_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getRudang($topic_id)
{
    global $db;
    $query = "select topic_id,topic_name,file_id,isused,topic_intro,topic_tag,created_time from " . tname('rudang') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        $topic['file'] = getfile($topic['file_id']);
        return $topic;
    }
    return array();
}

function getRudangDetail($topic_id)
{
    global $db;
    $query = "select * from " . tname('rudang') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        return $topic;
    }
    return array();
}


function getAllRudang($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select topic_id from " . tname('rudang');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by topic_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的topics
    while ($rows = $db->fetch_array($query)) {
        $topic = getRudang($rows['topic_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getGonggao($topic_id)
{
    global $db;
    $query = "select topic_id,topic_name,file_id,isused,topic_intro,topic_tag,created_time from " . tname('gonggao') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        $topic['file'] = getfile($topic['file_id']);
        return $topic;
    }
    return array();
}

function getGonggaoDetail($topic_id)
{
    global $db;
    $query = "select * from " . tname('gonggao') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        return $topic;
    }
    return array();
}


function getAllGonggao($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select topic_id from " . tname('gonggao');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by topic_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的topics
    while ($rows = $db->fetch_array($query)) {
        $topic = getGonggao($rows['topic_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getFengcai($topic_id)
{
    global $db;
    $query = "select topic_id,topic_name,file_id,isused,topic_intro,topic_tag,created_time from " . tname('fengcai') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        $topic['file'] = getfile($topic['file_id']);
        return $topic;
    }
    return array();
}

function getFengcaiDetail($topic_id)
{
    global $db;
    $query = "select * from " . tname('fengcai') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        return $topic;
    }
    return array();
}


function getAllFengcai($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select topic_id from " . tname('fengcai');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by topic_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的topics
    while ($rows = $db->fetch_array($query)) {
        $topic = getFengcai($rows['topic_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getSanhuikyike($topic_id)
{
    global $db;
    $query = "select topic_id,topic_name,file_id,isused,topic_intro,topic_tag,created_time from " . tname('sanhuiyike') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        $topic['file'] = getfile($topic['file_id']);
        return $topic;
    }
    return array();
}

function getSanhuikyikeDetail($topic_id)
{
    global $db;
    $query = "select * from " . tname('sanhuiyike') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        return $topic;
    }
    return array();
}


function getAllSanhuikyike($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select topic_id from " . tname('sanhuiyike');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by topic_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的topics
    while ($rows = $db->fetch_array($query)) {
        $topic = getSanhuikyike($rows['topic_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getJizhonghuodongri($topic_id)
{
    global $db;
    $query = "select topic_id,topic_name,file_id,isused,topic_intro,topic_tag,created_time from " . tname('jizhonghuodongri') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        $topic['file'] = getfile($topic['file_id']);
        return $topic;
    }
    return array();
}

function getJizhonghuodongriDetail($topic_id)
{
    global $db;
    $query = "select * from " . tname('jizhonghuodongri') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        return $topic;
    }
    return array();
}


function getAllJizhonghuodongri($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select topic_id from " . tname('jizhonghuodongri');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by topic_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的topics
    while ($rows = $db->fetch_array($query)) {
        $topic = getJizhonghuodongri($rows['topic_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getJingcaihuodong($topic_id)
{
    global $db;
    $query = "select topic_id,topic_name,file_id,isused,topic_intro,topic_tag,created_time from " . tname('jingcaihuodong') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        $topic['file'] = getfile($topic['file_id']);
        return $topic;
    }
    return array();
}

function getJingcaihuodongDetail($topic_id)
{
    global $db;
    $query = "select * from " . tname('jingcaihuodong') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        return $topic;
    }
    return array();
}


function getAllJingcaihuodong($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select topic_id from " . tname('jingcaihuodong');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by topic_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的topics
    while ($rows = $db->fetch_array($query)) {
        $topic = getJingcaihuodong($rows['topic_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getSixianghuibao($topic_id)
{
    global $db;
    $query = "select * from " . tname('sixianghuibao') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        $topic['files'] = getHuibaoFiles(0, "and s_id='".$topic['topic_id']."' and type=1");
        $topic['file'] = getfile($topic['file_id']);
        $topic['user'] = getfile($topic['user_id']);
        return $topic;
    }
    return array();
}


function getAllSixianghuibao($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select topic_id from " . tname('sixianghuibao');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by topic_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的topics
    while ($rows = $db->fetch_array($query)) {
        $topic = getSixianghuibao($rows['topic_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getXiangshanghuibao($topic_id)
{
    global $db;
    $query = "select * from " . tname('xiangshanghuibao') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        $topic['files'] = getHuibaoFiles(0, "and s_id='".$topic['topic_id']."' and type=0");
        $topic['file'] = getfile($topic['file_id']);
        $topic['user'] = getfile($topic['user_id']);
        return $topic;
    }
    return array();
}


function getAllXiangshanghuibao($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select topic_id from " . tname('xiangshanghuibao');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by topic_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的topics
    while ($rows = $db->fetch_array($query)) {
        $topic = getXiangshanghuibao($rows['topic_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getHuibaoFile($f_id)
{
    global $db;
    $query = "select * from " . tname('huibao_file') . " where f_id=" . $f_id;
    $user = $db->fetch_first($query);
    if ($user['f_id'] > 0 && $user['file_id'] > 0 && $user['is_del'] == 0) {
        $user['file'] = getfile($user['file_id']);
        return $user;
    }
    return array();
}

function getHuibaoFiles($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $users = array();
    if (empty($sql)) {
        $sql = "select f_id from " . tname('huibao_file');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by f_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的users
    while ($rows = $db->fetch_array($query)) {
        $user = getHuibaoFile($rows['f_id']);
        if ($user) {
            $users[] = $user;
        }
    }
    return $users;
}

function getQianshouxingdong($topic_id)
{
    global $db;
    $query = "select topic_id,topic_name,file_id,isused,topic_intro,topic_tag,created_time from " . tname('qianshouxingdong') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        $topic['file'] = getfile($topic['file_id']);
        return $topic;
    }
    return array();
}

function getQianshouxingdongDetail($topic_id)
{
    global $db;
    $query = "select * from " . tname('qianshouxingdong') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        return $topic;
    }
    return array();
}


function getAllQianshouxingdong($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select topic_id from " . tname('qianshouxingdong');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by topic_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的topics
    while ($rows = $db->fetch_array($query)) {
        $topic = getQianshouxingdong($rows['topic_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getGongzuodongtai($topic_id)
{
    global $db;
    $query = "select topic_id,topic_name,file_id,isused,topic_intro,topic_tag,created_time from " . tname('gongzuodongtai') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        $topic['file'] = getfile($topic['file_id']);
        return $topic;
    }
    return array();
}

function getGongzuodongtaiDetail($topic_id)
{
    global $db;
    $query = "select * from " . tname('gongzuodongtai') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        return $topic;
    }
    return array();
}


function getAllGongzuodongtai($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select topic_id from " . tname('gongzuodongtai');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by topic_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的topics
    while ($rows = $db->fetch_array($query)) {
        $topic = getGongzuodongtai($rows['topic_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getUser($user_id)
{
    global $db;
    $query = "select * from " . tname('user') . " where user_id=" . $user_id;
    $user = $db->fetch_first($query);
    $queryCount = "select user_id from " . tname('user') . " where jifen>'" . $user['jifen'] . "'";
    $userCount = $db->num_rows($db->query($queryCount)) + 1;
    if ($user['user_id'] > 0) {
        $user['avatar'] = getfile($user['file_id']);
        $user['jifenCount'] = $userCount;
        return $user;
    }
    return null;
}

function getUserJifen($user_id)
{
    global $db;
    $query = "select user_id, jifen from " . tname('user') . " where user_id=" . $user_id;
    $user = $db->fetch_first($query);
    if ($user['user_id'] > 0) {
        return $user['jifen'];
    }
    return 0;
}


function getAllUsers($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $users = array();
    if (empty($sql)) {
        $sql = "select user_id from " . tname('user');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by user_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的users
    while ($rows = $db->fetch_array($query)) {
        $user = getUser($rows['user_id']);
        if ($user) {
            $users[] = $user;
        }
    }
    return $users;
}

function getGangwei($gid)
{
    global $db;
    $query = "select * from " . tname('gangwei') . " where gid=" . $gid;
    $gangwei = $db->fetch_first($query);
    if ($gangwei['gid'] > 0) {
        return $gangwei;
    }
    return array();
}


function getAllGangweis($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $gangweis = array();
    if (empty($sql)) {
        $sql = "select gid from " . tname('gangwei');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by gid desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的gangweis
    while ($rows = $db->fetch_array($query)) {
        $gangwei = getGangwei($rows['gid']);
        if ($gangwei) {
            $gangweis[] = $gangwei;
        }
    }
    return $gangweis;
}

function getNeeds($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $users = array();
    if (empty($sql)) {
        $sql = "select n_id from " . tname('need');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by n_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的users
    while ($rows = $db->fetch_array($query)) {
        $user = getNeed($rows['n_id']);
        if ($user) {
            $users[] = $user;
        }
    }
    return $users;
}

function getNeed($n_id)
{
    global $db;
    $query = "select * from " . tname('need') . " where n_id=" . $n_id;
    $user = $db->fetch_first($query);
    if ($user['n_id'] > 0) {
        $user['user'] = getUser($user['user_id']);
        $user['files'] = getServiceFiles(0, "and n_id='".$user['n_id']."'");
        $user['users'] = getServiceUsers(0, "and n_id='".$user['n_id']."'");
        $user['topic'] = getBBSTopic($user['topic_id']);
        return $user;
    }
    return array();
}


function getServices($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $users = array();
    if (empty($sql)) {
        $sql = "select s_id from " . tname('service');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by s_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的users
    while ($rows = $db->fetch_array($query)) {
        $user = getService($rows['s_id']);
        if ($user) {
            $users[] = $user;
        }
    }
    return $users;
}

function getService($s_id)
{
    global $db;
    $query = "select * from " . tname('service') . " where s_id=" . $s_id;
    $user = $db->fetch_first($query);
    if ($user['s_id'] > 0) {
        $user['user'] = getUser($user['user_id']);
        $user['files'] = getServiceFiles(0, "and s_id='".$user['s_id']."'");
        $user['users'] = getServiceUsers(0, "and s_id='".$user['s_id']."'");
        $user['topic'] = getBBSTopic($user['topic_id']);
        return $user;
    }
    return array();
}

function getServiceUsers($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $users = array();
    if (empty($sql)) {
        $sql = "select f_id from " . tname('service_need_user');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by f_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的users
    while ($rows = $db->fetch_array($query)) {
        $user = getServiceUser($rows['f_id']);
        if ($user) {
            $users[] = $user;
        }
    }
    return $users;
}

function getServiceUser($f_id)
{
    global $db;
    $query = "select * from " . tname('service_need_user') . " where f_id=" . $f_id;
    $user = $db->fetch_first($query);
    if ($user['f_id'] > 0 && $user['user_id'] > 0 && $user['is_del'] == 0) {
        $user['user'] = getUser($user['user_id']);
        return $user;
    }
    return array();
}

function getServiceFiles($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $users = array();
    if (empty($sql)) {
        $sql = "select f_id from " . tname('service_need_file');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by f_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的users
    while ($rows = $db->fetch_array($query)) {
        $user = getServiceFile($rows['f_id']);
        if ($user) {
            $users[] = $user;
        }
    }
    return $users;
}

function getServiceFile($f_id)
{
    global $db;
    $query = "select * from " . tname('service_need_file') . " where f_id=" . $f_id;
    $user = $db->fetch_first($query);
    if ($user['f_id'] > 0 && $user['file_id'] > 0 && $user['is_del'] == 0) {
        $user['file'] = getfile($user['file_id']);
        return $user;
    }
    return array();
}

function getBBSTopic($topic_id)
{
    global $db;
    $query = "select * from " . tname('bbs_topic') . " where topic_id=" . $topic_id;
    $topic = $db->fetch_first($query);
    if ($topic['topic_id'] > 0) {
        $topic['user'] = getUser($topic['user_id']);
        return $topic;
    }
    return null;
}

function getAllBBSTopic($cursor = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select topic_id from " . tname('bbs_topic');
        if (!empty($container)) {
            $sql .= " where 1 " . $container;
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by topic_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql); //这里是所有的topics
    while ($rows = $db->fetch_array($query)) {
        $topic = getBBSTopic($rows['topic_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getBBSMessage($message_id)
{
    global $db;
    $query = "select * from " . tname('bbs_message') . " where message_id=" . $message_id;
    $topic = $db->fetch_first($query);
    if ($topic['message_id'] > 0) {
        $topic['user'] = getUser($topic['user_id']);
        $topic['file'] = getfile($topic['file_id']);
        $topic['mp3'] = getfile($topic['mp3_id']);
        return $topic;
    }
    return array();
}

function getAllBBSMessage($cursor = 0, $topic_id = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select message_id from " . tname('bbs_message');
        $sql .= " where 1 ";
        if (!empty($container)) {
            $sql .= $container;
        }
        if ($topic_id > 0) {
            $sql .= " and topic_id='" . $topic_id . "'";
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by message_id asc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql);
    while ($rows = $db->fetch_array($query)) {
        $topic = getBBSMessage($rows['message_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getFeedback($f_id)
{
    global $db;
    $query = "select * from " . tname('feedback') . " where f_id=" . $f_id;
    $topic = $db->fetch_first($query);
    if ($topic['f_id'] > 0) {
        $topic['files'] = getHuibaoFiles(0, "and s_id='".$topic['f_id']."' and type=2");
        $topic['user'] = getUser($topic['user_id']);
        return $topic;
    }
    return array();
}

function getAllFeedback($cursor = 0, $user_id = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select f_id from " . tname('feedback');
        $sql .= " where 1 ";
        if (!empty($container)) {
            $sql .= $container;
        }
        if ($user_id > 0) {
            $sql .= " and user_id='" . $user_id . "'";
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by f_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql);
    while ($rows = $db->fetch_array($query)) {
        $topic = getFeedback($rows['f_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getWeimengxiang($f_id)
{
    global $db;
    $query = "select * from " . tname('weimengxiang') . " where f_id=" . $f_id;
    $topic = $db->fetch_first($query);
    if ($topic['f_id'] > 0) {
        $topic['files'] = getHuibaoFiles(0, "and s_id='".$topic['f_id']."' and type=3");
        $topic['user'] = getUser($topic['user_id']);
        return $topic;
    }
    return array();
}

function getAllWeimengxiang($cursor = 0, $user_id = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select f_id from " . tname('weimengxiang');
        $sql .= " where 1 ";
        if (!empty($container)) {
            $sql .= $container;
        }
        if ($user_id > 0) {
            $sql .= " and user_id='" . $user_id . "'";
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by f_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql);
    while ($rows = $db->fetch_array($query)) {
        $topic = getWeimengxiang($rows['f_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getJifen($f_id)
{
    global $db;
    $query = "select * from " . tname('jifen') . " where j_id=" . $f_id;
    $topic = $db->fetch_first($query);
        return $topic;
}

function getAllJifen($cursor = 0, $user_id = 0, $container = '', $order = '', $pagesize = 100, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select j_id from " . tname('jifen');
        $sql .= " where 1 ";
        if (!empty($container)) {
            $sql .= $container;
        }
        if ($user_id > 0) {
            $sql .= " and user_id='" . $user_id . "'";
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by j_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql);
    while ($rows = $db->fetch_array($query)) {
        $topic = getJifen($rows['j_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function getSign($s_id)
{
    global $db;
    $query = "select * from " . tname('user_sign') . " where s_id=" . $s_id;
    $topic = $db->fetch_first($query);
        return $topic;
}

function getAllSign($cursor = 0, $user_id = 0, $container = '', $order = '', $pagesize = 31, $sql = '')
{
    global $db;
    $topics = array();
    if (empty($sql)) {
        $sql = "select s_id from " . tname('user_sign');
        $sql .= " where 1 ";
        if (!empty($container)) {
            $sql .= $container;
        }
        if ($user_id > 0) {
            $sql .= " and user_id='" . $user_id . "'";
        }
        if (!empty($order)) {
            $sql .= ' ' . $order;
        } else {
            $sql .= ' order by s_id desc ';
        }
        $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    }
    $query = $db->query($sql);
    while ($rows = $db->fetch_array($query)) {
        $topic = getSign($rows['s_id']);
        if ($topic) {
            $topics[] = $topic;
        }
    }
    return $topics;
}

function setUserJifen($user_id, $jifen = 0, $jifen_name = '', $jifen_desc = '') {
    global $db;
    if($user_id > 0) {
        $db->query("insert into " . tname("jifen") . "(`user_id`, `jifen`, `jifen_name`, `jifen_desc`, `created_at`)values('" . $user_id . "','" . $jifen . "','" . $jifen_name . "','" . $jifen_desc . "','" . time() . "')");
    }

}

/**
 * @param int $cursor
 * @param string $container
 * @param string $order
 * @param int $pagesize
 * @param string $sql
 * @return array
 *
 * 搜索 新闻，公告，学习
 *
 */
function getAllSearch($cursor = 0, $container = '', $order = '', $pagesize = 31, $sql = '')
{
    global $db;
    $topics = array();

    // 新闻
    $sql = "select topic_id from " . tname('news');
    if (!empty($container)) {
        $sql .= " where 1 " . $container;
    }
    if (!empty($order)) {
        $sql .= ' ' . $order;
    } else {
        $sql .= ' order by topic_id desc ';
    }
    $sql .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    $query = $db->query($sql); //这里是所有的topics
    while ($rows = $db->fetch_array($query)) {
        $topic = getNews($rows['topic_id']);
        if ($topic) {
            $topic['topic_tag'] = 1; // 新闻
            $topics[] = $topic;
        }
    }

    // 公告
    $sql2 = "select topic_id from " . tname('gonggao');
    if (!empty($container)) {
        $sql2 .= " where 1 " . $container;
    }
    if (!empty($order)) {
        $sql2 .= ' ' . $order;
    } else {
        $sql2 .= ' order by topic_id desc ';
    }
    $sql2 .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    $query2 = $db->query($sql2); //这里是所有的topics
    while ($rows2 = $db->fetch_array($query2)) {
        $topic2 = getGonggao($rows2['topic_id']);
        if ($topic2) {
            $topic2['topic_tag'] = 2; // 公告
            $topics[] = $topic2;
        }
    }

    // 学习
    $sql21 = "select topic_id from " . tname('xuexi');
    if (!empty($container)) {
        $sql21 .= " where 1 " . $container;
    }
    if (!empty($order)) {
        $sql21 .= ' ' . $order;
    } else {
        $sql21 .= ' order by topic_id desc ';
    }
    $sql21 .= " limit " . ($cursor * $pagesize) . "," . $pagesize;
    $query21 = $db->query($sql21); //这里是所有的topics
    while ($rows21 = $db->fetch_array($query21)) {
        $topic21 = getXuexi($rows21['topic_id']);
        if ($topic21) {
            $topic21['topic_tag'] = 3; // 学习
            $topics[] = $topic21;
        }
    }


    return $topics;
}

function getfile($file_id)
{
    global $db, $bidcmskey;
    $file = array();
    if ($file_id > 0) {
        $query = "select file_original as `key`,file_path as `path`,file_type as `type`,width,height from " . tname('file') . " where file_id=" . $file_id;
        $file = $db->fetch_first($query);
        if (empty($file)) {
            $file = array("key" => $bidcmskey, "type" => "image/jpeg", "width" => "180", "height" => "180");
        }
    } else {
        $file = array("key" => $bidcmskey, "type" => "image/jpeg", "width" => "180", "height" => "180");
    }

    $fileSuf = end(explode('.', $file['key']));

    switch ($fileSuf) {
        case "jpg":
        case "jpeg":
        case "gif":
        case "bmp":
        case "png":
            $fileType = 'image';
            break;
        default:
            $fileType = 'file';
            break;
    }

    $file['type'] = $fileType;

    $file['farm'] = 'farm1';
    $file['bucket'] = 'hbimg';
    $file['host'] = $GLOBALS['app']['scheme'] . "://" . $GLOBALS['app']['host'] . "/" . $file['path'];
    $file['frames'] = 1;
    return $file;
}